--[[
	Project:		TruRP
	Author:			Martin 'Ploo' Pucilowski
	File:			sh_items.lua
	Date:			02/2010
	Description:	Handles shared item related things
--]]

local function Load( path )
	ITEM = nil
	ITEM = {  }
	
	include( "items/" .. path )

	ITEM.Class = string.gsub( path, ".lua", "" )

	TRU.ItemData[ ITEM.Class ] = ITEM
end

local function Register( item )
	print("=== Registering item: " .. item.Class)

	base = GetItem( item.Base )
	while ( base != nil ) do
		print("== Inheriting " .. base.Class )
		table.Inherit( item, base )
		base = GetItem( base.Base )
	end
	
	if item.Class != "base_item" then
		print("== Inheriting base_item" )
		table.Inherit( item, GetItem( "base_item" ) )
	end
	
	--PrintTable( item )
end

function LoadItems()
	for _, f in pairs( file.FindInLua( "TruRP/gamemode/items/*.lua" ) ) do
		if SERVER then AddCSLuaFile( "items/" .. f ) end
		Load( f )
	end
end

function RegisterItems()
	for k, v in pairs( TRU.ItemData ) do
		Register( v )
	end
end

function GetItem( class )
	if not TRU.ItemData[ class ] then return nil end
	return TRU.ItemData[ class ]
end

function ValidItem( class )
	if not GetItem( class ) then return false end
	return true
end

function ValidWorldItem( ent )
	if not ValidEntity( ent ) then return false end
	if not ent.Item then return false end
	return true
end

LoadItems()
RegisterItems()